package com.liusha.quicksand.web;

import com.liusha.quicksand.po.Type;
import com.liusha.quicksand.service.BlogService;
import com.liusha.quicksand.service.TypeService;
import com.liusha.quicksand.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 一世长安一世欢
 * @Date: 2020/10/18
 * @Time: 15:01
 * @Description: No Description
 */

@Controller
public class BlogTypeController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/type")
    public String types(@PageableDefault(size = 30, sort = {"updatetime"}, direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog, Model model) {

        model.addAttribute("types", typeService.listType());

        model.addAttribute("page", blogService.listBlog(pageable, blog));

        return "types";
    }

    @GetMapping("/type/{id}")
    public String types(@PageableDefault(size = 30, sort = {"updatetime"}, direction = Sort.Direction.DESC)
                                Pageable pageable,
                        @PathVariable Long id, Model model) {

        List<Type> types = typeService.listTypeTop(10000);

        if (id == -1) {
            id = types.get(0).getId();
        }
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);
        model.addAttribute("types", types);
        model.addAttribute("page", blogService.listBlog(pageable, blogQuery));
        model.addAttribute("activeTypeId", id);

        model.addAttribute("type",typeService.listType());

        model.addAttribute("page",blogService.listBlog(pageable,blogQuery));

        return "types";
    }
}
